package com.ctgu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctgu.server.dao.entity.AttendanceGroupUser;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.server.dao.entity.User;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.mapper.AttendanceGroupUserMapper;
import com.ctgu.server.service.AttendanceGroupUserService;
import com.ctgu.server.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 考勤组与用户关系表 服务实现类
 * </p>
 *
 * @author ctgu
 * @since 2023-06-13
 */
@Service
public class AttendanceGroupUserServiceImpl extends ServiceImpl<AttendanceGroupUserMapper, AttendanceGroupUser> implements AttendanceGroupUserService {
    @Autowired
    private UserServiceImpl userService;
    public boolean is_admin(String userId,String groupId){
        //判断用户是否拥有权限
        LambdaQueryWrapper<AttendanceGroupUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttendanceGroupUser::getIs_admin,true).eq(AttendanceGroupUser::getUserId,userId).eq(AttendanceGroupUser::getGroupId,groupId);
        return this.count(lambdaQueryWrapper) != 0;
    }

    @Override
    public Result addAttendanceGroupUser(String opId, String groupId, List<String> userId) {
        //判断当前用户
        if (!is_admin(opId,groupId)) {
            return ResultUtil.error("权限不足！");
        }
        if (userService.countInUserList(userId)!=userId.size()) {
            return ResultUtil.error("错误的用户列表信息");
        }
        List<AttendanceGroupUser> attendanceGroupUserList=new LinkedList<>();
        for(String id:userId){
            AttendanceGroupUser attendanceGroupUser=new AttendanceGroupUser();
            attendanceGroupUser.setGroupId(groupId);
            attendanceGroupUser.setUserId(id);
            attendanceGroupUser.setIs_admin(false);
            attendanceGroupUser.setIs_join_attendance(true);
            attendanceGroupUserList.add(attendanceGroupUser);
        }
        this.saveBatch(attendanceGroupUserList,attendanceGroupUserList.size());
        return ResultUtil.success("请求成功");
    }

    @Override
    public Result deleteAttendanceGroupUser(String opId, String groupId, List<String> userId) {
        //判断当前用户
        if (!is_admin(opId,groupId)) {
            return ResultUtil.error("权限不足！");
        }
        LambdaQueryWrapper<AttendanceGroupUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttendanceGroupUser::getGroupId,groupId).in(AttendanceGroupUser::getUserId,userId);
        this.remove(lambdaQueryWrapper);
        return ResultUtil.success("操作成功");
    }

    @Override
    public Result updateAttendanceGroupUser(String opId, String groupId, List<AttendanceGroupUser> userId) {
        //判断当前用户
        if (!is_admin(opId,groupId)) {
            return ResultUtil.error("权限不足！");
        }
        this.updateBatchById(userId,userId.size());
        return ResultUtil.success();
    }

    @Override
    public Result listAttendanceGroupUser(String opId, String groupId) {
        //判断当前用户
        if (!is_admin(opId,groupId)) {
            return ResultUtil.error("权限不足！");
        }
        LambdaQueryWrapper<AttendanceGroupUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttendanceGroupUser::getGroupId,groupId);
        return ResultUtil.success(this.list(lambdaQueryWrapper));
    }
}
